import Vue from 'vue'
import Vuex from 'vuex'
import cache from '@/utils/cache'
import constants from '@/config/constants'
import utils from '@/utils/utils'
import { Toast } from 'vant'

import userApi from '@/api/userApi'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
	orderSetting: cache.localGet(constants.CACHE.ORDER_SETTING),  //订单配置信息
    token: cache.localGet(constants.CACHE.TOKEN),  //token信息
    user: cache.localGet(constants.CACHE.USER_INFO),  //用户信息
    userAttachInfo: cache.sessionGet(constants.CACHE.USER_ATTATH_INFO),  //用户附加信息
    cartList: cache.sessionGet(constants.CACHE.CART_LIST),  //购物车列表
    cartAllChecked: cache.sessionGet(constants.CACHE.CART_ALL_CHECKED), //购物车全选状态
    cartMoneyChecked: cache.sessionGet(constants.CACHE.CART_MONEY_CHECKED), //购物车选择金额
    selectAddressInfo: cache.sessionGet(constants.CACHE.SELECT_ADDRESS_INFO), //选择的收货地址信息
    searchKeyword: cache.sessionGet(constants.CACHE.SEARCH_KEYWORD) //搜索关键字
  },
  mutations: {
	/* 设置订单配置信息 */
	setOrderSetting (state, data) {
	  state.orderSetting = data;
	  cache.sessionSet(constants.CACHE.ORDER_SETTING, data);
	},
    /* 设置token */
    setToken (state, data) {
      state.token = data;
      cache.localSet(constants.CACHE.TOKEN, data);
      this.commit(constants.STORE.UPDATE_USER);
      this.commit(constants.STORE.UPDATE_CART_LIST);
    },
    /* 更新用户信息 */
    async updateUser (state){
      if(state.token){
        const userResult = await userApi.memberInfo(state.token.id);
        const attchResult = await userApi.memberAttachInfo(state.token.id);
        if(userResult.code == constants.SUCCESS && attchResult.code == constants.SUCCESS){
          state.user = userResult.data;
          cache.localSet(constants.CACHE.USER_INFO, userResult.data);
          state.userAttachInfo = attchResult.data;
          cache.sessionSet(constants.CACHE.USER_ATTATH_INFO, attchResult.data);
        }
      }
    },
    clearUser (state) {
      //移除token
      state.token = null;
      cache.localSet(constants.CACHE.TOKEN, null);
      //移除用户信息
      state.user = null;
      cache.localSet(constants.CACHE.USER_INFO, null);
      //移除用户附加信息
      state.userAttachInfo = null;
      cache.sessionSet(constants.CACHE.USER_ATTATH_INFO, null);
      //移除购物车信息
      this.commit(constants.STORE.CLEAR_CART_LIST)
      state.cartMoneyChecked = null;
      cache.sessionSet(constants.CACHE.CART_MONEY_CHECKED, null);
      state.cartAllChecked = false;
      cache.sessionSet(constants.CACHE.CART_ALL_CHECKED, false);
    },
    /* 更新、清除购物车 */
    async updateCartList (state){
      if(state.token){
        var cartCheckedNum = 0;
        //获取旧的购物车列表
        var oldCartList = state.cartList;
        //获取新的购物车列表
        const result = await userApi.cartList(state.token.id);
        if(result.code == constants.SUCCESS){
          var data = result.data;
          data.forEach(cart => {
            //默认不选中
            cart.checked = false;
            if(oldCartList){
              //保持原有选中状态
              oldCartList.forEach(oldCart => {
                if(oldCart.checked && oldCart.id == cart.id){
                  cart.checked = true;
                  cartCheckedNum += 1;
                }
              })
            }
          })
          //判断是否全部选中
          if(cartCheckedNum == data.length && cartCheckedNum != 0){
            state.cartAllChecked = true;
            cache.sessionSet(constants.CACHE.CART_ALL_CHECKED, true);
          }else{
            state.cartAllChecked = false;
            cache.sessionSet(constants.CACHE.CART_ALL_CHECKED, false);
          }
          //更新购物车列表
          state.cartList = data;
          cache.sessionSet(constants.CACHE.CART_LIST, data);
          //计算选中金额
          var cartMoneyChecked = 0;
          data.forEach(cart => {
            cartMoneyChecked += cart.checked ? cart.goodsPrice * cart.goodsNum : 0;
          })
          state.cartMoneyChecked = cartMoneyChecked;
          cache.sessionSet(constants.CACHE.CART_MONEY_CHECKED, cartMoneyChecked);
        }
      }
    },
    /* 清空购物车 */
    clearCartList (state){
      state.cartList = null;
      cache.sessionSet(constants.CACHE.CART_LIST, null);
      state.cartMoneyChecked = null;
      cache.sessionSet(constants.CACHE.CART_MONEY_CHECKED, null);
      state.cartAllChecked = null;
      cache.sessionSet(constants.CACHE.CART_ALL_CHECKED, null);
    },
    /* 选中购物车 */
    updateCartChecked (state, checkInfo){
      if(state.user){
        var cartCheckedNum = 0;
        var oldCartList = state.cartList ? state.cartList : [];
        //遍历购物车列表，设置单个购物车选中状态
        oldCartList.forEach(cart => {
          cart.checked = checkInfo.id == cart.id ? checkInfo.checked : cart.checked;
          cartCheckedNum += cart.checked ? 1 : 0;
        })
        //判断是否全部选中
        if(cartCheckedNum == oldCartList.length && cartCheckedNum != 0){
          state.cartAllChecked = true;
          cache.sessionSet(constants.CACHE.CART_ALL_CHECKED, true);
        }else{
          state.cartAllChecked = false;
          cache.sessionSet(constants.CACHE.CART_ALL_CHECKED, false);
        }
        //更新购物车列表
        state.cartList = oldCartList;
        cache.sessionSet(constants.CACHE.CART_LIST, oldCartList);
        //计算选中金额
        var cartMoneyChecked = 0;
        oldCartList.forEach(cart => {
          cartMoneyChecked += cart.checked ? cart.goodsPrice * cart.goodsNum : 0;
        })
        state.cartMoneyChecked = cartMoneyChecked;
        cache.sessionSet(constants.CACHE.CART_MONEY_CHECKED, cartMoneyChecked);
      }
    },
    /* 全选反选购物车 */
    allCartChecked (state){
      var oldCartList = state.cartList;
      //所有购物车反选
      oldCartList.forEach(cart => {
        cart.checked = !state.cartAllChecked;
      })
      //更新购物车列表
      state.cartList = oldCartList;
      cache.sessionSet(constants.CACHE.CART_LIST, oldCartList);
      //更新购物车全选状态
      state.cartAllChecked = !state.cartAllChecked;
      cache.sessionSet(constants.CACHE.CART_ALL_CHECKED, state.cartAllChecked);
      //计算选中金额
      var cartMoneyChecked = 0;
      oldCartList.forEach(cart => {
        cartMoneyChecked += cart.checked ? cart.goodsPrice * cart.goodsNum : 0;
      })
      state.cartMoneyChecked = cartMoneyChecked;
      cache.sessionSet(constants.CACHE.CART_MONEY_CHECKED, cartMoneyChecked);
    },
    /* 删除购物车 */
    async deleteCart (state) {
      //获取选中的购物车ids
      var cartIdsArray = new Array();
      var oldCartList = state.cartList;
      oldCartList.forEach(cart => {
        if(cart.checked){
          cartIdsArray.push(cart.id);
        }
      })
      if(cartIdsArray.length == 0){
        Toast.fail("请选择要删除的商品");
        return;
      }

      //删除商品
      const result = await userApi.deleteCart({cartIds: utils.join(cartIdsArray, ",")});
      if(result.code == constants.SUCCESS){
        this.commit(constants.STORE.UPDATE_CART_LIST);
      }
    },
    /* 更新选中的地址信息 */
    updateSelectAddressInfo (state, address){
      state.selectAddressInfo = address;
      cache.sessionSet(constants.CACHE.SELECT_ADDRESS_INFO, address);
    },
    /* 更新搜索关键字 */
    updateSearchKeyword (state, val){
      state.searchKeyword = val;
      cache.sessionSet(constants.CACHE.SEARCH_KEYWORD, val);
    }
  },
  actions: {
  },
  modules: {
  }
})
